import tkinter as tk
import time
from tkinter import font
from tkinter import ttk
from tkinter import messagebox

class DigitalClock :

    def __init__(self,window) :
        self.window = window
        self.window.title('Clock')
        self.window.attributes('-topmost',True)
        self.tsfont = font.Font(family = 'Microsoft YaHei',size=50)
        self.tifont = font.Font(family = 'Microsoft YaHei',size=40)
        self.tssfont = font.Font(family = 'Microsoft YaHei',size=20)
        self.time_label = tk.Label(window,text = time.strftime('time'),
                                    font=self.tsfont,bg='purple',fg='white')
        self.time_label.grid(row=0,column=0,columnspan=3)
        self.timing = tk.Button(window,text='Timing',height=2,width=18,command = self.timing)
        self.timing.grid(row=1,column=0)
        self.countdown = tk.Button(window,text='CountDown',height=2,width=18,command= self.countdowning)
        self.countdown.grid(row=1,column=1)
        self.update_screen()

    def update_screen(self) :
        nowtime = time.strftime('%H:%M:%S')
        self.time_label.config(text = nowtime)
        self.window.after(1000,self.update_screen)

    def timing(self) :
        self.timing_window = tk.Toplevel(self.window)
        self.timing_window.title('Timing')
        self.timing_window.attributes('-topmost',True)
        self.timing_label = tk.Label(self.timing_window,text = '00:00:00',
                                    font=self.tifont,bg='white',fg='black')
        self.timing_label.grid(row=0,column=0,columnspan=3)
        self.timing_start = tk.Button(self.timing_window,text='Start',
                                      height=1,width=12,command = self.start_time)
        self.timing_start.grid(row=1,column=0)
        self.timing_stop = tk.Button(self.timing_window,text='Stop',
                                     height=1,width=12,command = self.stop_time)
        self.timing_stop.grid(row=1,column=1)
        self.timing_clean = tk.Button(self.timing_window,text='Clean',
                                      height=1,width=12,command = self.clean_time)
        self.timing_clean.grid(row=1,column=2)
        self.running = False
        self.starttime = 0
        self.storage = 0
        
    def update_timing(self) :
        if self.running :
            if self.storage == 0 :
                self.goingtime = time.time() - self.starttime
            else :
                self.goingtime = time.time() - self.starttime + self.storage
            hourgoing = int(self.goingtime//3600)
            minutegoing = int((self.goingtime - hourgoing*3600)//60)
            secondgoing = int(self.goingtime - hourgoing*3600 - minutegoing*60)
            self.timing_label.config(text = f'{hourgoing:02d}:{minutegoing:02d}:{secondgoing:02d}')
            self.timing_window.after(1000,self.update_timing)
        
    def start_time(self) :
        if not self.running :
            self.running = True
            if self.starttime == 0 :
                self.starttime = time.time()
            self.update_timing()

    def stop_time(self) :
        self.running = False
        if self.starttime != 0 :
            self.storage = self.goingtime
        self.starttime = 0

    def clean_time(self) :
        self.starttime = 0
        self.running = False
        self.goingtime = 0
        self.storage = 0
        self.timing_label.config(text = '00:00:00')

    def countdowning(self) :
        self.countdown_window = tk.Toplevel(window)
        self.countdown_window.title('CountDown')
        self.countdown_window.attributes('-topmost',True)
        self.setlabel = tk.Label(self.countdown_window,text = 'set time',font=self.tssfont)
        self.setlabel.grid(row=0,column=0)
        self.scaleh = tk.Scale(self.countdown_window,from_=0,to=24,orient='horizontal')
        self.scalem = tk.Scale(self.countdown_window,from_=0,to=60,orient='horizontal')
        self.scales = tk.Scale(self.countdown_window,from_=0,to=60,orient='horizontal')
        self.hlabel = tk.Label(self.countdown_window,text = 'hour')
        self.mlabel = tk.Label(self.countdown_window,text = 'minute')
        self.slabel = tk.Label(self.countdown_window,text = 'second')
        self.scaleh.grid(row=1,column=0)
        self.hlabel.grid(row=1,column=1)
        self.scalem.grid(row=2,column=0)
        self.mlabel.grid(row=2,column=1)
        self.scales.grid(row=3,column=0)
        self.slabel.grid(row=3,column=1)
        self.startc = True
        self.setbutton = tk.Button(self.countdown_window,text = 'Set',height=1,width=20,command = self.start_countdown)
        self.setbutton.grid(row=4,column=0,columnspan=2,padx=5,pady=5)

    def start_countdown(self) :
        if self.startc == True :
            self.startc = False
            self.jindu = ttk.Progressbar(self.countdown_window,orient='horizontal',length=200,mode='determinate')
            self.jindu.grid(row=5,column=0,columnspan=2)
            self.timeall = int(self.scaleh.get())*3600 + int(self.scalem.get())*60 + int(self.scales.get())
            self.jindu['maximum'] = self.timeall
            self.jindu['value'] = self.timeall
            self.update_progress()

    def update_progress(self) :
        if self.timeall > 0 :
            self.timeall -= 1
            self.jindu['value'] = self.timeall
            self.countdown_window.after(1000,self.update_progress)
        elif self.timeall == 0 :
            self.mess = messagebox.showinfo('Notice','Countdown Ends')
            self.startc = True

window = tk.Tk()
windows = DigitalClock(window)
window.mainloop()